package com.xinbochuang.machine.admin.controller.message;


import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xinbochuang.machine.admin.domain.SysMessage;
import com.xinbochuang.machine.admin.service.ISysMessageService;
import com.xinbochuang.machine.common.domain.TableDataInfo;
import com.xinbochuang.machine.common.utils.AESUtils;
import com.xinbochuang.machine.framework.web.domain.AjaxResult;
import org.apache.ibatis.annotations.Update;
import org.springframework.web.bind.annotation.*;

import com.xinbochuang.machine.common.controller.BaseController;

import javax.annotation.Resource;

/**
 *
 * @author 张亚辉
 * @date 2021-06-22
 */
@RestController
@RequestMapping("/api/message")
public class SysMessageController extends BaseController {
    @Resource
    ISysMessageService sysMessageService;
    /**
     * 获取用户未读的消息数
     * @return
     */
    @GetMapping("/getCountByUserId")
    public AjaxResult findEquCabinetNodeByMacId(@RequestParam("userId")String userId){
        QueryWrapper<SysMessage> queryWrapper=new QueryWrapper<>();
        queryWrapper.lambda().eq(SysMessage::getUserId,userId).eq(SysMessage::getStatus,"0");
        return AjaxResult.data(sysMessageService.count(queryWrapper));
    }


    /**
     * 获取用户未读的消息数
     * @return
     */
    @GetMapping("/getListByUserId")
    public AjaxResult findEquCabinetNodeByMacId(@RequestParam("userId")String userId,@RequestParam("pageSize") int pageSize,@RequestParam("pageNum") int pageNum){
        QueryWrapper<SysMessage> queryWrapper=new QueryWrapper<>();
        queryWrapper.lambda().eq(SysMessage::getUserId,userId).orderByAsc(SysMessage::getStatus).orderByDesc(SysMessage::getCreateTime);
        Page<SysMessage> userPage=new Page(pageNum,pageSize);
        return AjaxResult.data(sysMessageService.page(userPage,queryWrapper));
    }



    /**
     * 获取用户未读的消息数
     * @return
     */
    @GetMapping("/getListByUserIdN")
    public AjaxResult findEquCabinetNodeByMacIdN(@RequestParam("userId")String userId,@RequestParam("pageSize") int pageSize,@RequestParam("pageNum") int pageNum){
        QueryWrapper<SysMessage> queryWrapper=new QueryWrapper<>();
        queryWrapper.lambda().eq(SysMessage::getUserId, AESUtils.decode(userId)).orderByAsc(SysMessage::getStatus).orderByDesc(SysMessage::getCreateTime);
        Page<SysMessage> userPage=new Page(pageNum,pageSize);
        return AjaxResult.data(AESUtils.encode(JSON.toJSONStringWithDateFormat(sysMessageService.page(userPage,queryWrapper),"yyyy-MM-dd HH:mm:ss")));
    }

    /**
     * 获取用户未读的消息数
     * @return
     */
    @GetMapping("/updateMessageRead")
    public AjaxResult updateMessageRead(@RequestParam("id")String id){
        UpdateWrapper<SysMessage> queryWrapper=new UpdateWrapper<>();
        queryWrapper.lambda().eq(SysMessage::getId,id).set(SysMessage::getStatus,"1");
        sysMessageService.update(queryWrapper);
        return AjaxResult.ok();
    }
}

